//
// Created by zhenduowang on 2020/9/10.
//
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>

static int count = 0;
static pthread_mutex_t mutex;

struct NativeWorkerArgs {
    int id;
    char* name;
};

static void* nativeThreadWork(void* args) {
    NativeWorkerArgs* nativeWorkerArgs = (NativeWorkerArgs*) args;
    for (int i = 0; i < 5; ++i) {
        printf("name=%s, id=%d, i=%d\n", nativeWorkerArgs->name, nativeWorkerArgs->id, i);
    }
    free(nativeWorkerArgs->name);
    delete nativeWorkerArgs;
    return (void*) 1;
}

void thread_lock_test() {

    if (0 != pthread_mutex_init(&mutex, NULL)) { // 如果参数为NULL等价于mutex = PTHREAD_MUTEX_INITIALIZER;
        printf("init mutex fail\n");
        goto exit;
    } else {
        printf("init mutex success\n");
    }
    if (0 != pthread_mutex_lock(&mutex)) {
        printf("lock mutex fail\n");
        goto exit;
    }

    // TODO 执行主流程

    if (0 != pthread_mutex_unlock(&mutex)) {
        printf("unlock mutex fail\n");
        goto exit;
    }
    if (0 != pthread_mutex_destroy(&mutex)) {
        printf("destroy mutex fail\n");
        goto exit;
    }

exit :
    printf("");
    return;
}

void thread_test() {
    pthread_t threadHandle[3];
    // 创建线程
    for (int i = 0; i < 3; ++i) {
        NativeWorkerArgs* nativeWorkerArgs = new NativeWorkerArgs();
        nativeWorkerArgs->id = i;
        nativeWorkerArgs->name = (char*)malloc(sizeof(char) * 20);
//        char num[10];
//        itoa(i, num, 10); // windows特有函数
//        strcat(nativeWorkerArgs->name, num);
        sprintf(nativeWorkerArgs->name, "thread-%d", i);
        int result = pthread_create(&threadHandle[i], NULL, nativeThreadWork, (void*) nativeWorkerArgs);
        if (0 != result) {
            printf("create thread %s fail!\n", nativeWorkerArgs->name);
            goto exit;
        } else{
            printf("create thread %s success!\n", nativeWorkerArgs->name);
        }
    }
    // 等待线程结束
    for (int i = 0; i < 3; ++i) {
        void* result = NULL;
        if (0 != pthread_join(threadHandle[i], &result)) {
            printf("thread %d execute fail!\n", i);
            goto exit;
        } else {
            printf("thread %d execute success! result=%d \n", i, (int)result);
        }
    }
exit:
    printf("finish\n");
    return;
}

int main() {
    printf("Hello C World!\n");
    struct tm *systemTime, *ptm;
    time_t nowTime, newTime;
    time(&nowTime);// nowTime = time(NULL);
    ptm = gmtime (&nowTime); //建 UTC 时间
    systemTime = localtime(&nowTime);
    printf("系统日期：%d-%d-%d %d:%d:%d\n", 1900 + systemTime->tm_year, systemTime->tm_mon + 1, systemTime->tm_mday, systemTime->tm_hour, systemTime->tm_min, systemTime->tm_sec);

    ptm = systemTime;
    newTime = mktime (ptm); //得UTC时间 年月日时分秒
    printf("%s ", ctime(&newTime)); //打印出 Fri Sep 11 20:21:28 2020
    newTime = newTime + 8 * 3600; //北京时间加8小时就是 Sat Sep 12 04:21:28 2020
    printf("%s ", ctime(&newTime));

    thread_test();\
    return 0;
}